/**
 * ProductsController
 * @Chunning.Xu
 * 2011 Nov 22, 2011 3:17:09 PM
 **/
package com.lux.product.controllers
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Cubic;
	import com.lux.product.models.CategoryModel;
	import com.lux.product.models.ProductModel;
	import com.lux.product.models.SeriesModel;
	import com.lux.product.swfaddress.SWFAddress;
	import com.lux.product.swfaddress.SWFAddressEvent;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.external.ExternalInterface;
	import flash.system.ApplicationDomain;
	import flash.text.Font;
	import flash.utils.ByteArray;
	
	public class CategoryController extends LuxViewController
	{
		//START.
		
		public static function convertByteToXML(byteClass:Class):XML
		{
			var bytes:ByteArray = new byteClass;
			var strData:String = bytes.readUTFBytes(bytes.length);
			
			XML.ignoreWhitespace = true;
			var xml:XML = new XML(strData);
			
			return xml;
		}
		
		[Embed(source="xml/products.xml",mimeType="application/octet-stream")]
		private var productsClass:Class;
		
		private var cateModel:CategoryModel;
		private var seriesController:SeriesManagerController;
		
		public var seriseView:Sprite;
		public var loading:MovieClip;
		
		private var currentLabel:String;
		
		public function CategoryController(view:Sprite)
		{
			self = this;
			super(view);
			
			initialize();
		}
		
		private function initialize():void
		{
			Font.registerFont(SimSunFont);
			
			loading.visible = true;
			loading.play();
			var prdsXML:XML = convertByteToXML(productsClass);
			
			var cateXML:XML = prdsXML["category"][0];
			for each(var child:XML in prdsXML.children())
			{
				if(child["@title"].toString() == view.stage.loaderInfo.parameters["category"] as String)
				{
					cateXML = child;
					break;
				}
			}
			
			cateModel = new CategoryModel;
			cateModel.setXML(cateXML);
			
			cateModel.addEventListener(Event.COMPLETE,dataInitedHandler);
		}
		
		private function dataInitedHandler(evt:Event):void
		{
			loading.visible = false;
			loading.stop();
			
			if(cateModel.series == null || cateModel.series.length <= 0)
				return;
			
			SWFAddress.addEventListener(SWFAddressEvent.INIT,initAddress);
		}
		
		private function initAddress(evt:Event):void
		{
			//init view controller;
			seriesController = new SeriesManagerController(seriseView,cateModel.series);
			seriesController.view.visible = true;
			seriesController.view.alpha = 0;
			TweenLite.to(seriesController.view,1.0,{ease:Cubic.easeInOut,alpha:1.0});
			
			SWFAddress.setStrict(false);
			setPath(SWFAddress.getValue())
			SWFAddress.addEventListener(SWFAddressEvent.CHANGE,function(evt:Event):void{setPath(SWFAddress.getValue());});
		}
		
		private function setPath(path:String):void
		{
			path = path.replace(/^\//,"");
			
			var arrURL:Array = path.split("/");
			var label:String = arrURL.length > 0 ? arrURL[0] : "";
			
			if(label == currentLabel)
				return;
			
			var matchingSModel:SeriesModel = currentLabel is String ? null : cateModel.series[0];
			var matchingPModel:ProductModel = null;
			
			for each(var sModel:SeriesModel in cateModel.series)
			{
				if(sModel.label == label)
				{
					matchingSModel = sModel;
					break;
				}
				
				for each(var pModel:ProductModel in sModel.products)
				{
					if(pModel.label == label)
					{
						matchingPModel = pModel;
						matchingSModel = sModel
						break;
					}
				}
			}
			
			if(matchingPModel == null && matchingSModel == null)
				matchingSModel = cateModel.series[0];
			
			currentLabel = matchingPModel != null ? matchingPModel.label : matchingSModel.label;
		 	seriesController.setSModelAndPModel(matchingSModel,matchingPModel);
		}
		
		protected override function initSubview(subview:DisplayObject):void
		{
			subview.visible = false;
			if(subview.name == "loading")
				(subview as MovieClip).stop();
		}
		
		//END
	}
}